package org.fas.classarray
{
	import flash.utils.ByteArray;

	public dynamic class ByteArrayArray extends Array
	{
		public function ByteArrayArray()
		{
			super();
		}
		public var currentIndex:int=0;
		public var currentPosition:int=0;
		public function c(_index:int):ByteArray{
			return this[_index] as ByteArray;
		}
		public function resetIndexAndPosition():void{
			this.currentIndex = 0;
			this.currentPosition = 0;
		}
		public function read(_length:int):ByteArray{
			var _realLength:int = 0;
			var _leaveLength:int = 0;
			var _ba:ByteArray = new ByteArray();
			var _oriBa:ByteArray = this.c(this.currentIndex);
			if(_oriBa){
				if((currentPosition+_length)>_oriBa.length){
					_realLength = _oriBa.length-currentPosition;
					_leaveLength = currentPosition+_length - _oriBa.length;
					_ba.writeBytes(_oriBa,currentPosition,_oriBa.length-currentPosition);
				}else{
					_realLength = _length;
					_leaveLength = 0;
				}
				_ba.writeBytes(_oriBa,currentPosition,_realLength);
				currentPosition +=_length;
				if(currentPosition>=_oriBa.length){
					currentPosition=0;
					currentIndex++;
				}
				return _ba;
			}
			return null;
		}
	}
}